define(['jquery', 'text!../template/suggestion.html', 'css!../css/suggestion', 'common'], function ($, tplSuggestion) {
    let _suggestion = {};
    let currentPage = 1;
    let tpls = {
        'tplSuggestion': _.template(tplSuggestion)
    };

    _suggestion.show = function () {
        currentPage = 1;
        setTable(0, 5);
    }

    function setTable(from, count) {
        Ajax('/xjrest/advice/page?from=' + from + '&count=' + count, setTableBack);
    }

    function reloadTable() {
        currentPage = 1;
        setTable(0, 5);
    }

    window.showImg = function (src) {
        $('.showImgbox').attr('src', src);
        $('.showScaleImg').show();
    }

    window.showScaleImg = function (src) {
        $('.showScaleImg').hide();
    }

    function setTableBack(data) {
        if (data.ret.success) {
            let _data = data.ret.value;
            $('#contant').html(tpls.tplSuggestion({
                message: _data
            }));

            let totals = data.ret.total;
            if (totals == 0) {
                $("#noRecordMessage").show();
            } else {
                $("#noRecordMessage").hide();
            }
            if (0 == totals % 5) {
                totalPage = parseInt(totals / 5);
                if (totalPage == 0) {
                    totalPage = 1;
                }
            } else {
                totalPage = parseInt(totals / 5) + 1;
            }
            $('#pagination').paginations({
                currentPage: currentPage,
                totalPage: totalPage,
                callback: function (current) {
                    currentPage = current;
                    current = (current - 1) * 5;
                    setTable(current, 5);
                }
            });
        }
    }

    return _suggestion;
})